export function isObject (obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}
export function isArray (arr) {
  return Array.isArray(arr)
}

export function merge(target, ...arg) {
  return arg.reduce((acc, cur) => {
    return Object.keys(cur).reduce((subAcc, key) => {
      const srcVal = cur[key]
      if (isObject(srcVal)) {
        subAcc[key] = merge(subAcc[key] ? subAcc[key] : {}, srcVal)
      } else if (isArray(srcVal)) {
        subAcc[key] = srcVal.map((item, idx) => {
          if (isObject(item)) {
            const curAccVal = subAcc[key] ? subAcc[key] : []
            return merge(curAccVal[idx] ? curAccVal[idx] : {}, item)
          } else {
            return item
          }
        })
      } else {
        subAcc[key] = srcVal
      }
      return subAcc
    }, acc)
  }, target)
}

export function getSubjection(routers, componentName) {
  for(let i = 0; i < routers.length; i++){
    const routes = routers[i];
    if(routes){
      if(routes.component && routes.component == componentName){
        return routes.subjection;
      } else if(routes.children){
        return getSubjection(routes.children, componentName)
      }
    }
  }
  return ''
}

export function getSimulator(nav, path){
  const paths = path.split('/');
  if(nav.length > 0){
    for(let i = 0; i < nav.length; i++){
      const curItem = nav[i];
      const innerItem = curItem.items;
      if(innerItem.length > 0){
        for(let j = 0; j < innerItem.length; j++ ){
          const curInnerItem = innerItem[j];
          if(paths.indexOf(curInnerItem.path) !== -1){
            if(curItem.hideInSimulator || curInnerItem.hideInSimulator){
              return true
            }
          }
        }
      }
    }
  }
  return false;
}

export function isMobile(){
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  );
}